package com.lzq.leetcode.hoot100;

import java.util.Arrays;

public class s75 {
    public void sortColors(int[] nums) {

        int l = 0;
        int r = nums.length-1;
        int p = 0;

        while (p <= r) {
            if (nums[p] == 0) {
                swap(nums, l++, p);
            } else if (nums[p] == 2) {
                swap(nums, r--, p);
            } else {
                p++;
            }
            if ( p < l) {
                p++;
            }
        }
    }

    private void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];;
        nums[j] = temp;
    }

    public static void main(String[] args) {
        s75 s75 = new s75();
        int[] nums = new int[]{2,0,2,1,1,0};
        s75.sortColors(nums);
        System.out.println(Arrays.toString(nums));
    }
}
